//
//  UIImageFactory.m
//  SalaryiPhone
//
//  Created by shaowei on 13-3-13.
//  Copyright (c) 2013年 vin. All rights reserved.
//

#import "UIImageFactory.h"

@implementation UIImageFactory

/**
 获取可以自由伸展的图片，ios6只可以横向伸展，竖向有bug
 @param imageName:png，jpg图片可以不带扩展名，但是推荐带上扩展名，文件未找到，则候选搜索@2x文件
 @returns nil
 */
+ (UIImage *)getFreeImageWithName:(NSString *)imageName{
    UIImage *imageTemp = [UIImageFactory getBundleImageWithName:imageName];
    
    CGSize imageSize = imageTemp.size;
    if (OS_VERSION < 6.0f) {
        //4.3以下的系统
        imageTemp = [imageTemp stretchableImageWithLeftCapWidth:imageSize.width*0.5f
                                                   topCapHeight:imageSize.height*0.5f];
    }else{
        UIEdgeInsets edgeInsets = UIEdgeInsetsMake(imageSize.height*0.5f,
                                                   imageSize.width*0.5f,
                                                   imageSize.height*0.5f,
                                                   imageSize.width*0.5f);
        imageTemp = [imageTemp resizableImageWithCapInsets:edgeInsets];
    }
    
    return imageTemp;
}

/**
 由文件名创建UIImage，查找路径：mainBundle
 @param imageName:png，jpg图片可以不带扩展名，但是推荐带上扩展名，文件未找到，则候选搜索@2x文件
 @returns UIImage
 */
+ (UIImage *)getBundleImageWithName:(NSString *)imageName{
    
    NSString *imagePath = [UIImageFactory getBundleImagePathWithName:imageName];
    return [UIImage imageWithContentsOfFile:imagePath];
}

/**
 由图片名找到图片的路径String,查找路径：mainBundle
 @param imageName:png，jpg图片可以不带扩展名，但是推荐带上扩展名，文件未找到，则候选搜索@2x文件
 @returns 图片的路径String
 */
+ (NSString *)getBundleImagePathWithName:(NSString *)imageName{
    
    NSString *imagePath = nil;
    NSString *fileName = nil;
    NSString *extensionName = nil;
    
    //分割文件名与扩展名
    NSArray *arrString = [imageName componentsSeparatedByString:@"."];
    switch ([arrString count]) {
        case 1:
        {
            fileName = imageName;
            extensionName = @"png";
        }
            break;
        case 2:
        {
            //处理有扩展名的情况
            fileName = [arrString objectAtIndex:0];
            extensionName = [arrString objectAtIndex:1];
        }
            break;
            
        default:
            return imagePath;
            break;
    }

    imagePath = [[NSBundle mainBundle] pathForResource:fileName ofType:extensionName];
    
    //处理少加@2x的情况
    if (!imagePath) {
        imagePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@@2x",imageName] ofType:extensionName];
    }
    
    //处理扩展名jpg与png写错的情况
    if (!imagePath) {
        extensionName = [extensionName isEqualToString:@"png"]?@"jpg":@"png";
        imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:extensionName];
    }
    
    
    if (!imagePath) {
        DLog(@"SW - %@：不存在",imageName);
    }
    
    return imagePath;
    
}

#pragma mark - 图片处理

+ (UIImage *)scaleJPGImage:(UIImage *)jpgImage toRectScale:(CGFloat)rectScaleSize toDataScale:(CGFloat)dataScaleSize
{
    UIImage *scaledImage = nil;
    if (fabs(rectScaleSize - 1.0f) > 0.01f) {
        UIGraphicsBeginImageContext(CGSizeMake(jpgImage.size.width*rectScaleSize,jpgImage.size.height*rectScaleSize));
        [jpgImage drawInRect:CGRectMake(0, 0, jpgImage.size.width * rectScaleSize, jpgImage.size.height *rectScaleSize)];
        scaledImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }else{
        scaledImage = jpgImage;
    }
    
    
    if ((1.0f - dataScaleSize)>0.01f && fabsf(dataScaleSize - 0.0f)>0.01f) {
        NSData *tempData = UIImageJPEGRepresentation(scaledImage, dataScaleSize);
        scaledImage = [UIImage imageWithData:tempData];
    }
    
    return scaledImage;
}




@end
